package cc.xsubj.trading.game;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cc.xsubj.engine.Globals;
import cc.xsubj.engine.ResourceManager;

import org.lwjgl.opengl.Display;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.tests.TransitionTest;

import cc.xsubj.trading.world.GrassWorld;


public class TradingGameMain extends StateBasedGame {

    public static final int SCREEN_HEIGHT = 600;
    public static final int SCREEN_WIDTH = 800;
    private static Map<String, Integer> nameToID = new HashMap<String, Integer>();
    private static int nextID = 0;
    private static TradingGameMain instance;
    
	private TradingGameMain(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}
	
	public static TradingGameMain instance(String name)
	{
	    if (instance == null)
	        instance = new TradingGameMain(name);
	    return instance;
	}
	
	public static TradingGameMain instance()
	{
	    return instance("");
	}
	
	private int getNextID()
	{
	    nextID++;
	    return nextID;
	}
	
	private GrassWorld createGrassWorld(String name, GameContainer container)
	{
	    int id = getNextID();
	    GrassWorld world = new GrassWorld(id, container, name);
	    nameToID.put(name, id);
	    return world;
	}
	
	public void changeWorld(String name)
	{
	    this.enterState(nameToID.get(name), new FadeOutTransition(), new FadeInTransition());
	}
	
	
	@Override
	public void initStatesList(GameContainer container) throws SlickException {
		
		try {
			ResourceManager.loadResources("data/resources.xml");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		addState(createGrassWorld("grassworld", container));
		addState(createGrassWorld("grassworld2", container));
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			
			AppGameContainer container = new AppGameContainer(TradingGameMain.instance("TradingGame"));
			container.setDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, false);
			container.setTargetFrameRate(60);
			//container.setIcon("data/images/icon.png");
			Globals.scaleX = 2;
			Globals.scaleY = 2;
			//Globals.debugEnabled = true;
			container.start();
			
		} catch (SlickException e) {
			e.printStackTrace();
		}

	}

}
